package com.ljm.sys.dict.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljm.common.enums.DataStatus;
import com.ljm.common.enums.ResultStatus;
import com.ljm.common.excuption.Assert;
import com.ljm.common.page.PageDto;
import com.ljm.common.util.BeanUtils;
import com.ljm.common.util.StringUtil;
import com.ljm.sys.dict.api.SysDictApi;
import com.ljm.sys.dict.dto.SysDictDto;
import com.ljm.sys.dict.entity.SysDict;
import com.ljm.sys.dict.mapper.SysDictMapper;
import com.ljm.sys.dict.service.ISysDictService;
import com.ljm.sys.org.dto.SysOrgUserDto;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * <p>
    * 系统字典 服务实现类
    * </p>
 *
 * @author HuamingChen
 * @since 2019-02-14
 */
@Service
@Transactional
public class SysDictServiceImpl extends ServiceImpl<SysDictMapper, SysDict>
        implements ISysDictService, SysDictApi {

    @Override
    public void save(SysDictDto dto, SysOrgUserDto userDto){
        SysDict po=new SysDict();
        BeanUtils.copyProperties(dto,po);
        if(StringUtil.isEmpty(dto.getId())){
            po.setCreateTime(LocalDateTime.now());
            po.setCreater(userDto.getLoginName());
            po.setStatus(DataStatus.NORMAL.getCode());
            po.setOrgId(userDto.getOrgId());
        }
        po.setEditor(userDto.getLoginName());
        po.setEditTime(LocalDateTime.now());
        this.saveOrUpdate(po);
    }

    @Override
    public PageDto<SysDictDto> getByPage(String dictKey, String dictValue, int pageNo, int pageSize){
        PageDto<SysDictDto> pageDto=new PageDto<>(pageNo,pageSize);
        IPage<SysDict> page=new Page<>(pageNo,pageSize);
        QueryWrapper<SysDict> wrapper=new QueryWrapper<>();
        if(!StringUtil.isEmpty(dictKey)){
            wrapper.like("dict_key",dictKey);
        }
        if(!StringUtil.isEmpty(dictValue)){
            wrapper.like("dict_value",dictValue);
        }
        wrapper.orderByDesc("edit_time");
        page=this.page(page,wrapper);
        if(!StringUtil.isEmpty(page.getRecords())){
            pageDto.setTotal(page.getTotal());
            pageDto.setRecords(BeanUtils.copyPropertiesByList(page.getRecords(),SysDictDto.class));
        }
        return pageDto;
    }

    @Override
    public SysDictDto getDataById(Long id){
        if(StringUtil.isEmpty(id)){
            Assert.throwException(ResultStatus.FIAL.getCode(),"主键为空");
        }
        SysDictDto dto=new SysDictDto();
        SysDict po=this.getById(id);
        if(!StringUtil.isEmpty(po)){
            BeanUtils.copyProperties(po,dto);
        }
        return dto;
    }

    @Override
    public void del(Long id){
        if(StringUtil.isEmpty(id)){
            Assert.throwException(ResultStatus.FIAL.getCode(),"主键为空");
        }
        this.removeById(id);
    }
}
